package org.example.caiwumanage.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.caiwumanage.entity.Employee;
import org.example.caiwumanage.entity.EmployeeLoginVO;
import org.example.caiwumanage.properties.BaseContext;
import org.example.caiwumanage.properties.JwtClaimsConstant;
import org.example.caiwumanage.properties.JwtProperties;
import org.example.caiwumanage.result.Result;
import org.example.caiwumanage.service.EmployeeService;
import org.example.caiwumanage.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     *
     * @param employeeLoginDTO (用户名可以是username/phone)
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Employee employeeLoginDTO) {
        log.info("用户登录：{}", employeeLoginDTO);
        System.out.println("当前线程"+Thread.currentThread().getId());
        Employee employee = employeeService.login(employeeLoginDTO);
        if(employee==null){
            return Result.error("您输入的用户信息不存在(账号或密码错误)！");
        }
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 注册员工
     *
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody Employee employee) {
        log.info("员工注册：{}", employee);
        employeeService.register(employee);
        return Result.success();
    }

    /**
     * 完善员工信息列表(页面回显)
     *
     * @return
     */
    @GetMapping("/perfect")
    public Result perfectList() {
        long id= BaseContext.getCurrentId();//获取jwt校验时向线程内传入的id
        log.info("获取完善员工信息列表：{}", id);
        return Result.success(employeeService.perfectList(id));
    }

    /**
     * 完善员工信息列表(信息修改)
     *
     * @return
     */
    @PostMapping("/perfect")
    public Result perfect(@RequestBody Employee employee) {
        long id= BaseContext.getCurrentId();//获取jwt校验时向线程内传入的id
        employee.setId(id);
        log.info("完善员工信息：{}", employee);
        employeeService.perfect(employee);
        return Result.success();
    }

    /**
     * 忘记密码(手机号，身份证号，新的密码)
     *jwt校验不拦截
     * @return
     */
    @PostMapping("/forgetPassword")
    public Result forgotPassword(@RequestBody Employee employee) {
        log.info("��记密码：{}", employee);
        employeeService.forgotPassword(employee);
        return Result.success();
    }



}
